package pers.elonnsu.springbootmall.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import pers.elonnsu.springbootmall.common.Constant;
import pers.elonnsu.springbootmall.exception.MallExceptionEnum;
import pers.elonnsu.springbootmall.model.pojo.User;
import pers.elonnsu.springbootmall.service.UserService;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

public class AdminFilter implements Filter {
    @Autowired
    private UserService userService;

    private final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpSession session = request.getSession();
        servletResponse.setContentType("application/json;charset=utf-8");
        User currentUser = (User) session.getAttribute(Constant.MALL_USER);

        logger.info("HTTP_METHOD: " + request.getMethod());
        logger.info("IP: " + request.getRemoteAddr());

        String result = null;
        if (currentUser == null) {
            PrintWriter out = new HttpServletResponseWrapper((HttpServletResponse) servletResponse).getWriter();
            result = "{\n" +
                    "    \"status\": " + MallExceptionEnum.NEED_LOGIN.getCode() + ",\n" +
                    "    \"msg\": \"" + MallExceptionEnum.NEED_LOGIN.getMessage() + "\",\n" +
                    "    \"data\": null\n" +
                    "}";
            logger.info("RESPONSE: " + new ObjectMapper().writeValueAsString(result));
            logger.info("=======================================================");
            out.write(result);
            out.flush();
            out.close();
            return;
        }

        // 校验是否为管理员，是则继续操作执行
        if (userService.isAdminRole(currentUser)) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            PrintWriter out = new HttpServletResponseWrapper((HttpServletResponse) servletResponse).getWriter();
            result = "{\n" +
                    "    \"status\": " + MallExceptionEnum.NEED_ADMIN_LOGIN.getCode() + ",\n" +
                    "    \"msg\": \"" + MallExceptionEnum.NEED_ADMIN_LOGIN.getMessage() + "\",\n" +
                    "    \"data\": null\n" +
                    "}";
            logger.info("RESPONSE: " + new ObjectMapper().writeValueAsString(result));
            logger.info("=======================================================");
            out.write(result);
            out.flush();
            out.close();
        }
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
